package com.compass.doctor.ui.mine

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager

import com.compass.doctor.databinding.ActivityInquiryRecordBinding
import com.compass.doctor.ui.mine.adapter.InquiryRecordAdapter
import com.compass.doctor.ui.mine.viewmodel.MineViewModel
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.ui.patient.PatientChatActivity
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.dpToPx
import com.netease.yunxin.kit.corekit.im.model.UserInfo
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 问诊记录
 * Date:2023/11/16 14:07
 */
class InquiryRecordActivity : BaseVBActivity<ActivityInquiryRecordBinding>(), OnRefreshListener,
    OnLoadMoreListener {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, InquiryRecordActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val mineViewModel: MineViewModel by viewModels()
    private var mPage = 1
    private lateinit var mAdapter: InquiryRecordAdapter

    override fun initView(savedInstanceState: Bundle?) {
        initRecyclerView()
    }

    override fun initData() {
        showLoading()
        getConsultationList()
        mineViewModel.recordLiveData.observe(this){
            if (mPage == 1) {
                mAdapter.setData(it)
                mBinding.refreshLayout.finishRefresh()
                mBinding.refreshLayout.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding.emptyView.visible()
                } else {
                    mBinding.emptyView.gone()
                }
            } else {
                mBinding.refreshLayout.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mAdapter.addAll(it)
                } else {
                    mBinding.refreshLayout.setEnableLoadMore(false)
                }
            }



            dismissLoading()
        }
    }


    private fun getConsultationList() {
        mineViewModel.getConsultationList(mPage)
    }


    private fun initRecyclerView() {
        mBinding.refreshLayout.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@InquiryRecordActivity)
            setOnLoadMoreListener(this@InquiryRecordActivity)
        }
        mAdapter = InquiryRecordAdapter()
        val dp12 = dpToPx(12)
        mBinding.recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(left = dp12, top = dp12, right = dp12, bottom = dp12)
                setLastBottom(true)

            })
            adapter = mAdapter
        }
        mAdapter.onItemClickListener = { _: View, position: Int ->
            val item = mAdapter.getItem(position)

//            item?.let {
//                PatientChatActivity.start(
//                    this,
//                    UserInfo(
//                        it.patientWyyxAccid,
//                        it.patientName,
//                        it.patientAvatar
//                    ),
//                    it.patientId
//                )
//
//            }

        }

    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getConsultationList()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getConsultationList()
    }
}